package com.feather.tenia.adapter;

import com.feather.common.db.Connector;
import com.feather.tenia.adapter.util.CloseUtil;
import com.feather.tenia.JdbcProperty;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * @author flogyin
 */
public class JdbcPropertyImpl implements JdbcProperty {
	protected int		driverMajorVersion;
	protected int		driverMinorVersion;
	protected String	driverName;
	protected String	driverVersion;

	// protected int databaseMajorVersion;
	// protected int databaseMinorVersion;
	protected String	databaseProductName;
	protected String	databaseProductVersion;

	// protected int jdbcMajorVersion;
	// protected int jdbcMinorVersion;

	protected String	catalogSeparator;
	protected String	userName;
	protected String	catalog;
	protected String	shema;

	protected String	url;

	protected String	implement;

	private JdbcPropertyImpl() {
	}

	public static JdbcProperty instance(Connector connector) {
		if (connector == null) {
			return null;
		}
		Connection conn = null;
		try {
			conn = connector.getConnection();
			DatabaseMetaData dbMetaData = conn.getMetaData();
			if (dbMetaData == null) {
				throw new RuntimeException("连接无法取到 " + DatabaseMetaData.class.getName() + ".");
			}
			JdbcPropertyImpl property = new JdbcPropertyImpl();

			property.driverMajorVersion = dbMetaData.getDriverMajorVersion();
			property.driverMinorVersion = dbMetaData.getDriverMinorVersion();
			property.driverName = dbMetaData.getDriverName();
			property.driverVersion = dbMetaData.getDriverVersion();
			// property.catalog = conn.getCatalog();
			// property.shema = conn.getSchema();

			// property.databaseMajorVersion =
			// dbMetaData.getDatabaseMajorVersion();
			// property.databaseMinorVersion =
			// dbMetaData.getDatabaseMinorVersion();
			property.databaseProductName = dbMetaData.getDatabaseProductName();
			property.databaseProductVersion = dbMetaData.getDatabaseProductVersion();

			// property.jdbcMajorVersion = dbMetaData.getJDBCMajorVersion();
			// property.jdbcMinorVersion = dbMetaData.getJDBCMinorVersion();

			property.catalogSeparator = dbMetaData.getDatabaseProductName();
			property.userName = dbMetaData.getUserName();

			property.url = dbMetaData.getURL();

			property.implement = conn.getClass().getName();

			return property;
		} catch (RuntimeException ex) {
			throw ex;
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		} finally {
			CloseUtil.close(conn);
		}
	}

	@Override
	public int getDriverMajorVersion() {
		return this.driverMajorVersion;
	}

	@Override
	public int getDriverMinorVersion() {
		return this.driverMinorVersion;
	}

	@Override
	public String getDriverName() {
		return this.driverName;
	}

	@Override
	public String getDriverVersion() {
		return this.driverVersion;
	}

	@Override
	public String getDatabaseProductName() {
		return this.databaseProductName;
	}

	@Override
	public String getDatabaseProductVersion() {
		return this.databaseProductVersion;
	}

	@Override
	public String getCatalogSeparator() {
		return this.catalogSeparator;
	}

	@Override
	public String getUserName() {
		return this.userName;
	}

	@Override
	public String getURL() {
		return this.url;
	}

	@Override
	public String getConnectionImplement() {
		return this.implement;
	}
}
